package com.funo.logic.generate.code.logic;

import com.funo.logic.constant.CodeConstants;
import com.funo.logic.generate.code.AbstractCodeSupport;
import com.funo.logic.generate.code.CodeSupport;
import com.funo.logic.generate.code.MethodFieldCodeSupport;
import com.funo.logic.generate.extand.VariableArgument;
import com.funo.logic.model.LogicItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 逻辑if代码生成构建器
 * @since 2024年7月25日20:54:35
 */
public class LogicIfCodeSupport extends AbstractCodeSupport implements CodeSupport {

    /**
     * 逻辑类型：if/else/for/while···
     */
    private String logicType;
    /**
     *
     */
    private VariableArgument variableArgument;
    /**
     * 具体逻辑配置对象
     * 逻辑表达式 ==、>、<=、>=、!=、&&、||、isNull、isNotNull
     */
    private List<LogicItem> logicItemList = new ArrayList<>();
    /**
     * 内部逻辑
     */
    private MethodFieldCodeSupport methodFieldCodeSupport;

    @Override
    public String generateCode() {
        codeBuffer.append(logicType);
        addOpenParen();
        codeBuffer.append(variableArgument.getVariableName());
        addSpace();
        // TODO 逻辑多个判断条件
//        for (LogicItem logicItem : logicItemList) {
//            if (logicItem.getExpression().equals("isNull")) {
//                codeBuffer.append("== null");
//            } else if (logicItem.getExpression().equals("isNotNull")) {
//                codeBuffer.append("!= null");
//            } else {
//                codeBuffer.append(logicItem.getExpression());
//            }
//        }
        // demo 暂时就做第一个条件
        LogicItem logicItem = logicItemList.get(0);
        if (logicItem.getExpression().equals("isNull")) {
            codeBuffer.append("== null");
        } else if (logicItem.getExpression().equals("isNotNull")) {
            codeBuffer.append("!= null");
        } else {
            codeBuffer.append(logicItem.getExpression());
        }
        addCloseParen();
        addSpace();
        // 添加大括号
        addOpenBrace();
        addNewLine();
        // 添加if 内部业务代码
        if (methodFieldCodeSupport != null) {
            addIndent();
            addIndent();
            codeBuffer.append(methodFieldCodeSupport.generateCode());
        }
        addNewLine();
        addIndent();
        addIndent();
        addCloseBrace();
        return codeBuffer.toString();
    }

    @Override
    public String getName() {
        return "";
    }

    public void addLogicItem(LogicItem logicItem) {
        logicItemList.add(logicItem);
    }
    public void addLogicItemList(List<LogicItem> items) {
        logicItemList.addAll(items);
    }
    public void setVariableArgument(VariableArgument variableArgument) {
        this.variableArgument = variableArgument;
    }

    public void setLogicType(String logicType) {
        this.logicType = logicType;
    }

    public void setMethodFieldCodeSupport(MethodFieldCodeSupport methodFieldCodeSupport) {
        this.methodFieldCodeSupport = methodFieldCodeSupport;
    }
}
